<?php
class JPush_API 
{
    
    public function alert($targets, $title, $message, $targetUrl, $startActivity, $builder_id)
    {
        $url  = 'https://api.jpush.cn/v3/push';
        $data = array(
            'platform' => array('android'),
            'audience' => $targets,
            'notification' => array(
                'android' => array(
                    'title' => $title,
                    'alert' => $message,
                    'builder_id' => $builder_id,
                    'extras' => array(
                        'url' => $targetUrl, 
                        'startActivity' => $startActivity,
                    ),
                ),
            ),
        );

        $result = $this->post($url, $data);
        return $result;
    }

    public function setAlias ($regId, $alias)
    {
        $url  = 'https://device.jpush.cn/v3/devices/' . rawurlencode($regId);
        $data = array(
            'alias' => $alias,
        );

        $result = $this->post($url, $data);
        return $result;
    }

    public function setTags ($regId, $arrAddTags = NULL, $arrDelTags = NULL)
    {
        $url  = 'https://device.jpush.cn/v3/devices/' . rawurlencode($regId);
        $tags = array();

        if (! empty($arrAddTags))
            $tags['add'] = $arrAddTags;
        if (! empty($arrDelTags))
            $tags['remove'] = $arrDelTags;

        if (empty($tags))
            return FALSE;

        $data = array(
            'tags' => $tags,
        );

        $result = $this->post($url, $data);
        return $result;
    }

    public function getAlias ($regId)
    {
        $url  = 'https://device.jpush.cn/v3/devices/'.rawurlencode($regId);
        $result = $this->get($url);
        return $result;
    }

    private function get ($url) 
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, FALSE);

        $base64Auth = $this->getAuthString();
        $headerArr = array("Authorization: Basic $base64Auth");
        curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr );

        $json_info = curl_exec($ch);

        if (curl_errno($ch)) {
            Kohana::$log->add(LOG::ERROR, "CURL_ERROR:".curl_error($ch));
            return NULL;
        }

        curl_close($ch);

        $result = @json_decode($json_info, TRUE);
        return $result;

    }


    private function post ($url, $array_param) 
    {
        $post_str_data = json_encode($array_param);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str_data);

        $base64Auth = $this->getAuthString();
        $headerArr = array("Authorization: Basic $base64Auth");
        curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr );

        $json_info = curl_exec($ch);

        if (curl_errno($ch)) {
            Kohana::$log->add(LOG::ERROR, "CURL_ERROR:".curl_error($ch));
            return NULL;
        }

        curl_close($ch);

        $result = @json_decode($json_info, TRUE);
        return $result;

    }

    private function getAuthString ()
    {
        $appKey = Kohana::config('jpush.appKey');
        $appSec = Kohana::config('jpush.masterSecret');

        return base64_encode("$appKey:$appSec");
    }
}

